home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / Hungry Homer / CW Source / SpriteTools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  7.4 KB  |  322 lines  |  [TEXT/MMCC]

  1. #include "SpriteTools.h"
  2.  
  3. WindowPtr     myWindow;
  4. SpritePtr    gSpriteList = nil;
  5. GrafPtr     gOffScreen, gBackScreen;
  6.  
  7.  
  8. #include <QDOffScreen.h>
  9.  
  10. void MyNewGWorld(GrafPtr *offscreenGWorld, Rect *boundsRect)
  11. {
  12.     GDHandle saveGD;
  13.     GWorldPtr savePort;
  14.  
  15.     GetGWorld(&savePort, &saveGD);
  16.  
  17.     if ( noErr != NewGWorld((GWorldPtr *)offscreenGWorld, 0, boundsRect, nil, nil, pixelsLocked) )
  18.         DoError;
  19.     if ( LockPixels((*(CGrafPtr *)offscreenGWorld)->portPixMap) )
  20.         ;
  21.     SetGWorld(savePort, saveGD);
  22. };
  23.  
  24.  
  25.  
  26. GrafPtr LoadFaceFromCicn(short cicnId)
  27. {
  28.     GrafPtr offscreenGWorld;
  29.     CIconHandle theCicn;
  30.     GDHandle saveGD;
  31.     GWorldPtr savePort;
  32.  
  33.     GetGWorld(&savePort, &saveGD);
  34.     theCicn = GetCIcon(cicnId);
  35.     MyNewGWorld(&offscreenGWorld, &(**theCicn).iconMask.bounds);
  36.     if ( offscreenGWorld != nil )
  37.     {
  38.         SetGWorld((GWorldPtr)offscreenGWorld, nil);
  39.         PlotCIcon(&(**theCicn).iconMask.bounds, theCicn);
  40.  
  41.         if ( offscreenGWorld == nil )
  42.             offscreenGWorld->clipRgn = NewRgn ();
  43.         if ( noErr != BitMapToRegion(offscreenGWorld->clipRgn, &(**theCicn).iconMask) )
  44.             offscreenGWorld->clipRgn = nil;
  45.  
  46.         DisposeCIcon(theCicn);
  47.     }
  48.     SetGWorld(savePort, saveGD);
  49.     return offscreenGWorld;
  50. }
  51.  
  52. GrafPtr    LoadFaceFromText(short ResID, short Which)
  53. {
  54.     FontInfo     info;
  55.     CIconHandle theCicn;
  56.     Str255         URL;
  57.     Rect        URLRect;
  58.     short        URLWidth,URLHeight;
  59.     GrafPtr     offscreenGWorld;
  60.     GDHandle     saveGD;
  61.     GWorldPtr    URLMask;
  62.     GWorldPtr     savePort;
  63.     RGBColor    black = {0x0000,0x0000,0x0000};
  64.     RGBColor    white = {0xFFFF,0xFFFF,0xFFFF};
  65.     URLHeight = 12;
  66.     
  67.     
  68.     theCicn = GetCIcon(130);
  69.     
  70.     TextSize(URLHeight);
  71.     GetFontInfo(&info);
  72.     URLHeight = info.ascent + info.descent;
  73.     
  74.     GetGWorld(&savePort, &saveGD);
  75.     GetIndString(URL, ResID, Which);
  76.     URLWidth = TextWidth(&URL,0,255);
  77.     SetRect(&URLRect, 0,0,URLWidth,URLHeight);
  78.     MyNewGWorld(&offscreenGWorld, &URLRect);
  79.     MyNewGWorld(&(GrafPtr)URLMask,&URLRect);
  80.     if ( offscreenGWorld != nil )
  81.     {
  82.         SetGWorld((GWorldPtr)URLMask, nil);
  83.         RGBForeColor(&black);
  84.         PaintRect(&URLRect);
  85.         RGBForeColor(&white);        
  86.         DrawString(URL);
  87.         
  88.         SetGWorld((GWorldPtr)offscreenGWorld, nil);
  89.         RGBForeColor(&white);
  90.         PaintRect(&URLRect);
  91.         RGBForeColor(&black);
  92.         DrawString(URL);
  93.  
  94.         if ( offscreenGWorld == nil )
  95.             offscreenGWorld->clipRgn = NewRgn ();
  96.         if ( noErr != BitMapToRegion(offscreenGWorld->clipRgn, &(**theCicn).iconMask))
  97.             offscreenGWorld->clipRgn = nil;
  98.     }
  99.     SetGWorld(savePort, saveGD);
  100.     return offscreenGWorld;
  101. }
  102.     
  103.  
  104. static RgnHandle gTmpRgn = nil;
  105.  
  106. void PlotFace(GrafPtr theCicn, GrafPtr destPort, Point where)
  107. {
  108.     GDHandle saveGD;
  109.     GWorldPtr savePort;
  110.     Rect bounds;
  111.     RGBColor saveForeColor, saveBackColor;
  112.  
  113.     GetGWorld(&savePort, &saveGD);
  114.     bounds = theCicn->portRect;
  115.     OffsetRect(&bounds, where.h - bounds.left, where.v - bounds.top);
  116.  
  117.     if ( gTmpRgn == nil )
  118.         gTmpRgn = NewRgn ();
  119.     CopyRgn(theCicn->clipRgn, gTmpRgn);
  120.     OffsetRgn(gTmpRgn, where.h, where.v);
  121.     SetPort(destPort);
  122.     GetForeColor(&saveForeColor);
  123.     GetBackColor(&saveBackColor);
  124.     ForeColor(blackColor);
  125.     BackColor(whiteColor);
  126.     CopyBits(&theCicn->portBits, &destPort->portBits, &theCicn->portRect, &bounds, srcCopy, gTmpRgn);
  127.     RGBForeColor(&saveForeColor);
  128.     RGBBackColor(&saveBackColor);
  129.     SetGWorld(savePort, saveGD);
  130. }
  131.  
  132. SpritePtr NewSprite()
  133. {
  134.     SpritePtr    who;
  135.  
  136.     who = (SpritePtr) NewPtr(sizeof(SpriteRecord));
  137.     if (who == nil) return nil;
  138.     if (gSpriteList != nil)
  139.     {
  140.         gSpriteList->prev = who;
  141.     }
  142.     who->next = gSpriteList;
  143.     who->prev = nil;
  144.     gSpriteList = who;
  145.     return who;
  146. }
  147.  
  148.  
  149. void DisposeSprite(SpritePtr who)
  150. {
  151.     if (who == nil) return;
  152.     if (who->next != nil)
  153.         who->next->prev = who->prev;
  154.     if (who->prev != nil)
  155.         who->prev->next = who->next;
  156.     if (who == gSpriteList)
  157.         gSpriteList = who->next;
  158.     DisposePtr((Ptr)who);
  159. }
  160.  
  161. Boolean KeepOnScreen(SpritePtr theSprite)
  162. {
  163.     Boolean    returnValue = false;
  164.     
  165.     if (theSprite->position.h < 0)
  166.     {
  167.         theSprite->position.h = 0;
  168.         theSprite->speed.h = abs(theSprite->speed.h);
  169.         returnValue = true;
  170.     }
  171.     if (theSprite->position.v < 0)
  172.     {
  173.         theSprite->position.v = 0;
  174.         theSprite->speed.v = abs(theSprite->speed.v);
  175.         returnValue = true;
  176.     }
  177.     if (theSprite->position.h > gOffScreen->portRect.right - theSprite->face->portRect.right)
  178.     {
  179.         theSprite->position.h = gOffScreen->portRect.right - theSprite->face->portRect.right;
  180.         theSprite->speed.h = -abs(theSprite->speed.h);
  181.         returnValue = true;
  182.     }
  183.     if (theSprite->position.v > gOffScreen->portRect.bottom - theSprite->face->portRect.bottom)
  184.     {
  185.         theSprite->position.v = gOffScreen->portRect.bottom - theSprite->face->portRect.bottom;
  186.         theSprite->speed.v = -abs(theSprite->speed.v);
  187.         returnValue = true;
  188.     }
  189.     
  190.     return returnValue;
  191. }
  192.  
  193.  
  194. #ifdef _hasfixedpoint
  195.  
  196. Boolean KeepOnScreenFixed(SpritePtr theSprite)
  197. {
  198.     Boolean    returnValue = false;
  199.  
  200.     if (theSprite->position.h < 0)
  201.     {
  202.         theSprite->position.h = 0;
  203.         theSprite->fixedPointPosition.h = 0;
  204.         theSprite->speed.h = abs(theSprite->speed.h);
  205.         returnValue = true;
  206.     }
  207.     if (theSprite->position.v < 0)
  208.     {
  209.         theSprite->position.v = 0;
  210.         theSprite->fixedPointPosition.v = 0;
  211.         theSprite->speed.v = abs(theSprite->speed.v);
  212.         returnValue = true;
  213.     }
  214.     if (theSprite->position.h > gOffScreen->portRect.right - theSprite->face->portRect.right)
  215.     {
  216.         theSprite->position.h = gOffScreen->portRect.right - theSprite->face->portRect.right;
  217.         theSprite->fixedPointPosition.h = theSprite->position.h << 4;
  218.         theSprite->speed.h = -abs(theSprite->speed.h);
  219.         returnValue = true;
  220.     }
  221.     if (theSprite->position.v > gOffScreen->portRect.bottom - theSprite->face->portRect.bottom)
  222.     {
  223.         theSprite->position.v = gOffScreen->portRect.bottom - theSprite->face->portRect.bottom;
  224.         theSprite->fixedPointPosition.v = theSprite->position.v << 4;
  225.         theSprite->speed.v = -abs(theSprite->speed.v);
  226.         returnValue = true;
  227.     }
  228.     
  229.     return returnValue;
  230. }
  231. #endif
  232.  
  233.  
  234. short RectSeparate(SpritePtr theSprite, SpritePtr anotherSprite)
  235. {
  236.     short    distance[4], shortest, shortestDistance, i;
  237.     Rect    bounds1, bounds2;
  238.  
  239.     bounds1 = theSprite->face->portRect;
  240.     OffsetRect(&bounds1, theSprite->position.h, theSprite->position.v);
  241.  
  242.     bounds2 = anotherSprite->face->portRect;
  243.     OffsetRect(&bounds2, anotherSprite->position.h, anotherSprite->position.v);
  244.  
  245.     distance[0] = bounds2.top - bounds1.bottom;
  246.     distance[1] = bounds2.bottom - bounds1.top;
  247.     distance[2] = bounds2.right - bounds1.left;
  248.     distance[3] = bounds2.left - bounds1.right; 
  249.  
  250.     shortest = 0;
  251.     shortestDistance = abs(distance[0]);
  252.     for (i=1; i<4; i++)
  253.     {
  254.         if (abs(distance[i]) < shortestDistance)
  255.         {
  256.             shortest = i;
  257.             shortestDistance = abs(distance[i]);
  258.         }
  259.     }
  260.  
  261.     switch (shortest)
  262.     {
  263.         case 0:
  264.         case 1:
  265.             theSprite->position.v += distance[shortest]; break;
  266.         case 2:
  267.         case 3:
  268.             theSprite->position.h += distance[shortest]; break;
  269.     }
  270.     return shortest;
  271. }
  272.  
  273.  
  274. short Rand(short range)
  275. {
  276.     short roll;
  277.     
  278.     roll = Random();
  279.     return (abs(roll) % range);
  280. }
  281.  
  282.  
  283.  
  284. Boolean    RegionHit(SpritePtr theSprite, SpritePtr anotherSprite)
  285. {
  286.     RgnHandle    faceRegion1, faceRegion2;
  287.     Boolean    result;
  288.  
  289.     faceRegion1 = NewRgn();
  290.     faceRegion2 = NewRgn();
  291.  
  292.     CopyRgn(theSprite->face->clipRgn, faceRegion1);
  293.     OffsetRgn(faceRegion1, theSprite->position.h, theSprite->position.v);
  294.  
  295.     CopyRgn(anotherSprite->face->clipRgn, faceRegion2);
  296.     OffsetRgn(faceRegion2, anotherSprite->position.h, anotherSprite->position.v);
  297.     
  298.     SectRgn(faceRegion1, faceRegion2, faceRegion1);
  299.     result = !EmptyRgn(faceRegion1);
  300.     
  301.     DisposeRgn(faceRegion1);
  302.     DisposeRgn(faceRegion2);
  303.     
  304.     return    result;
  305. }
  306.  
  307.  
  308. void SplitVector(Point v, Point d, Point *p, Point *n)
  309. {
  310.     long length2, dotProduct;
  311.  
  312.     length2 = d.h * d.h + d.v * d.v;
  313.  
  314.     dotProduct = v.h * d.h + v.v * d.v;
  315.  
  316.     (*p).h = d.h * dotProduct / length2;
  317.     (*p).v = d.v * dotProduct / length2;
  318.     (*n).h = v.h - (*p).h;
  319.     (*n).v = v.v - (*p).v;
  320. }
  321.  
  322.